<?php
/*
 module:		职位表
 create_time:	2024-03-14 09:14:48
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\model\Jobs as JobsModel;
use think\exception\ValidateException;

class Jobs extends Common
{

    /*start*/
    /**
     * @api {post} /Jobs/noLoginList 03、不登录列表
     */
    function noLoginList()
    {
        if (!$this->request->isPost()) {
            throw new ValidateException('请求错误');
        }
        $limit = $this->request->post('limit', 20, 'intval');
        $page = $this->request->post('page', 1, 'intval');
        $list_type = $this->request->post('list_type', 1, 'intval');//1-首页默认；2-热门
        $sort_type = $this->request->post('sort_type', 1, 'intval');//1-最新；2-附近；
        $district = $this->request->post('district', "");
        $latitude = $this->request->post('lat', "");
        $longitude = $this->request->post('lng', "");
        $jobs_name = $this->request->post('jobs_name', "");
        $wage_id = $this->request->post('wage_id', 0, 'intval');
        $education_id = $this->request->post('education_id', 0, 'intval');
        $experience_id = $this->request->post('experience_id', 0, 'intval');
        $uptime_id = $this->request->post('uptime_id', 0, 'intval');
        $jobtag_id = $this->request->post('jobtag_id', "");
        $scale_id = $this->request->post('scale_id', 0, 'intval');
        $subclass = $this->request->post('subclass', 0, 'intval');
        $company_id = $this->request->post('company_id', 0, 'intval');
        $field = 'id,jobs_name,companyname,company_id,nature_cn,sex_cn,age,topclass,category,subclass,category_cn,minwage,maxwage,trade_cn,district_cn,education_cn,experience_cn,tag_cn,is_rec_index,refreshtime,negotiable';
        $sort = 'id desc';
        $where = [];
        if ($list_type == 2) {
            $where["is_rec_index"] = 1;
        }
        if ($sort_type == 1) {
            $sort = 'refreshtime desc,emergency desc';
        } elseif ($sort_type == 2) {
            if (!empty($latitude) && !empty($longitude)) {
                $field .= ",ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-`lat`*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(`lat`*PI()/180)*POW(SIN(({$longitude}*PI()/180-`map_x`*PI()/180)/2),2)))*1000) AS distance";
                $sort = "distance asc,emergency desc";
            }
        }

        $query = JobsModel::field($field);
        if (!empty($district)) {
            $district = mb_substr($district, 0, 2);
            $where['district_cn'] = ['like', $district];
        }
        if (!empty($jobs_name)) {
            $where['jobs_name'] = ['like', $jobs_name];
        }
        if (!empty($wage_id)) {
            $wageInfo = newWage($wage_id);
            if (!empty($wageInfo)) {
                $query = $query->where("minwage", ">=", $wageInfo["minwage"]);
                $query = $query->where("maxwage", "<=", $wageInfo["maxwage"]);
            }
        }
        if (!empty($education_id)) {
            $query = $query->whereIn("education", [0, $education_id]);
        }
        if (!empty($experience_id)) {
            $query = $query->whereIn("experience", [0, $experience_id]);
        }
        if (!empty($uptime_id)) {
            $uptime = upTime($uptime_id);
            if (!empty($uptime)) {
                $stime = strtotime("-{$uptime['day']} days", time());
                $stime = strtotime(date("Y-m-d", $stime) . " 00:00:00");
                $where['refreshtime'] = ['between', [$stime, time()]];
            }
        }
        if (!empty($jobtag_id)) {
            $jobtag_id = explode(",", $jobtag_id);
            foreach ($jobtag_id as $v) {
                $query = $query->where("FIND_IN_SET('{$v}',tag)");
            }
        }
        if (!empty($scale_id)) {
            $query = $query->whereIn("scale", [0, $scale_id]);
        }
        if (!empty($subclass)) {
            $query = $query->where("subclass", $subclass);
        }
        if (!empty($company_id)) {
            $query = $query->where("company_id", $company_id);
        }
        $query = $query->where(formatWhere($where));
        $query = $query->where("audit", 1);
        $query = $query->where("display", 1);
        $query = $query->where("company_audit", 1);
        $query = $query->order($sort);
        $res = $query->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
        if (!empty($res["data"])) {
            $list = $res["data"];
            foreach ($list as &$item) {
                $item = $this->getItem($item);
            }
            $res["data"] = $list;
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }

    /**
     * @api {post} /Jobs/index 01、首页数据列表
     */
    function index()
    {
        if (!$this->request->isPost()) {
            throw new ValidateException('请求错误');
        }
        $limit = $this->request->post('limit', 20, 'intval');
        $page = $this->request->post('page', 1, 'intval');
        $list_type = $this->request->post('list_type', 1, 'intval');//1-首页默认；2-热门
        $sort_type = $this->request->post('sort_type', 1, 'intval');//1-最新；2-附近；
        $district = $this->request->post('district', "");
        $latitude = $this->request->post('lat', "");
        $longitude = $this->request->post('lng', "");
        $jobs_name = $this->request->post('jobs_name', "");
        $wage_id = $this->request->post('wage_id', 0, 'intval');
        $education_id = $this->request->post('education_id', 0, 'intval');
        $experience_id = $this->request->post('experience_id', 0, 'intval');
        $uptime_id = $this->request->post('uptime_id', 0, 'intval');
        $jobtag_id = $this->request->post('jobtag_id', "");
        $scale_id = $this->request->post('scale_id', 0, 'intval');
        $subclass = $this->request->post('subclass', 0, 'intval');
        $company_id = $this->request->post('company_id', 0, 'intval');
        $field = 'id,jobs_name,companyname,company_id,nature_cn,sex_cn,age,topclass,category,subclass,category_cn,minwage,maxwage,trade_cn,district_cn,education_cn,experience_cn,tag_cn,is_rec_index,refreshtime,negotiable';
        $sort = 'id desc';
        $uid = $this->request->uid;
        $where = [];
        if ($list_type == 2) {
            $where["is_rec_index"] = 1;
        }
        if ($sort_type == 1) {
            $sort = 'refreshtime desc,emergency desc';
        } elseif ($sort_type == 2) {
            if (!empty($latitude) && !empty($longitude)) {
                $field .= ",ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-`lat`*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(`lat`*PI()/180)*POW(SIN(({$longitude}*PI()/180-`map_x`*PI()/180)/2),2)))*1000) AS distance";
                $sort = "distance asc,emergency desc";
            }
        }

        $query = JobsModel::field($field);

        if (!empty($wage_id)) {
            $wageInfo = newWage($wage_id);
            if (!empty($wageInfo)) {
                $query = $query->where("minwage", ">=", $wageInfo["minwage"]);
                $query = $query->where("maxwage", "<=", $wageInfo["maxwage"]);
            }
        }
        if (!empty($education_id)) {
            $query = $query->whereIn("education", [0, $education_id]);
        }
        if (!empty($experience_id)) {
            $query = $query->whereIn("experience", [0, $experience_id]);
        }
        if (!empty($uptime_id)) {
            $uptime = upTime($uptime_id);
            if (!empty($uptime)) {
                $stime = strtotime("-{$uptime['day']} days", time());
                $stime = strtotime(date("Y-m-d", $stime) . " 00:00:00");
                $where['refreshtime'] = ['between', [$stime, time()]];
            }
        }
        if (!empty($jobtag_id)) {
            $jobtag_id = explode(",", $jobtag_id);
            foreach ($jobtag_id as $v) {
                $query = $query->where("FIND_IN_SET('{$v}',tag)");
            }
        }
        if (!empty($scale_id)) {
            $query = $query->whereIn("scale", [0, $scale_id]);
        }
        if (!empty($subclass)) {
            $query = $query->where("subclass", $subclass);
        }
        if (!empty($company_id)) {
            $query = $query->where("company_id", $company_id);
        }
        if (!empty($jobs_name)) {
            $where['jobs_name'] = ['like', $jobs_name];
        } else {
            if (!empty($uid) && empty($company_id) && $list_type == 1) {
                $newJobsList = \app\api\model\Resume::where("uid", $uid)->column("intention_jobs_id");
                $newJobsIds = "";
                foreach ($newJobsList as $v) {
                    $newJobsIds = $newJobsIds . "." . $v;
                }
                $newJobsIds = explode(".", $newJobsIds);
                $newJobsIds = array_unique($newJobsIds);
                foreach ($newJobsIds as $key => $v) {
                    if (empty($v)) unset($newJobsIds[$key]);
                }
                $newJobsIds = array_merge($newJobsIds);
                if (!empty($newJobsIds)) {
                    $query = $query->whereIn("subclass", $newJobsIds);
                } else {
//                    $query = $query->where("subclass", "=", "-1");
                }
            }
            if (!empty($district)) {
                $district = mb_substr($district, 0, 2);
                $where['district_cn'] = ['like', $district];
            }
        }
        $query = $query->where(formatWhere($where));
        $query = $query->where("audit", 1);
        $query = $query->where("display", 1);
        $query = $query->where("company_audit", 1);
        $query = $query->order($sort);
        $res = $query->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
        if (!empty($res["data"])) {
            $list = $res["data"];
            foreach ($list as &$item) {
                $item = $this->getItem($item);
            }
            $res["data"] = $list;
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }

    public function getItem($item)
    {
        $item["refreshtime"] = timeAnalysis2($item["refreshtime"]);
        if (empty($item["tag_cn"]) || $item["tag_cn"] == "请选择") {
            $item["tag_cn"] = [];
        } else {
            $item["tag_cn"] = explode(",", $item["tag_cn"]);
        }
        if (!empty($item["contents"])) {
            $item["contents"] = htmlspecialchars_decode($item["contents"]);
        }
        if (!empty($item["distance"])) {
            $item["distance"] = sprintf("%.2f", $item["distance"] / 1000) . "KM";
        }

        return $item;
    }

    /**
     * @api {get} /Jobs/view 02、查看详情
     */
    function view()
    {
        $data['id'] = $this->request->post('id', '', 'serach_in');
        $latitude = $this->request->post('lat', "");
        $longitude = $this->request->post('lng', "");
        $field = 'id,jobs_name,companyname,company_id,nature_cn,sex_cn,age,topclass,category,subclass,category_cn,minwage,maxwage,trade_cn,district_cn,education_cn,experience_cn,tag_cn,refreshtime,click,lng,lat,contents,is_rec_index,negotiable';
        if (!empty($latitude) && !empty($longitude)) {
            $field .= ",ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-`lat`*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(`lat`*PI()/180)*POW(SIN(({$longitude}*PI()/180-`lng`*PI()/180)/2),2)))*1000) AS distance";
        }
        $res = JobsModel::field($field)->where($data)->findOrEmpty()->toArray();
        JobsModel::where($data)->inc("click", 1)->update();
        $res = $this->getItem($res);
        $uid = $this->request->uid;
        $res["is_collection"] = 0;
        $res["is_apply_jobs"] = 0;
        if (!empty($uid)) {
            $where = [];
            $where['jobs_id'] = $res['id'];
            $where['personal_uid'] = $uid;
            $info = \app\api\model\Personalfavorites::where($where)->findOrEmpty()->toArray();
            if (!empty($info)) {
                $res["is_collection"] = 1;
            }
            $info = \app\api\model\Personaljobsapply::where(["jobs_id" => $res['id'], "personal_uid" => $uid])->find();
            if (!empty($info)) {
                $res["is_apply_jobs"] = 1;
            }
        }
        $res["apply_num"] = \app\api\model\Personaljobsapply::where(["jobs_id" => $res['id']])->count();

        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }

    /**
     * @api {post} /Jobs/noLoginView 04、不登录详情
     */
    function noLoginView()
    {
        $data['id'] = $this->request->post('id', '', 'serach_in');
        $latitude = $this->request->post('lat', "");
        $longitude = $this->request->post('lng', "");
        $field = 'id,jobs_name,companyname,company_id,nature_cn,sex_cn,age,topclass,category,subclass,category_cn,minwage,maxwage,trade_cn,district_cn,education_cn,experience_cn,tag_cn,refreshtime,click,lng,lat,contents,is_rec_index,negotiable';
        if (!empty($latitude) && !empty($longitude)) {
            $field .= ",ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-`lat`*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(`lat`*PI()/180)*POW(SIN(({$longitude}*PI()/180-`lng`*PI()/180)/2),2)))*1000) AS distance";
        }
        $res = JobsModel::field($field)->where($data)->findOrEmpty()->toArray();
        JobsModel::where($data)->inc("click", 1)->update();
        $res = $this->getItem($res);
        $res["is_collection"] = 0;
        $res["is_apply_jobs"] = 0;
        $res["apply_num"] = \app\api\model\Personaljobsapply::where(["jobs_id" => $res['id']])->count();
        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }
    /*end*/

}

